package com.tec.modules.order.service.impl;

import com.tec.modules.order.domain.BizOrder;
import com.tec.modules.order.service.dto.BizOrderDto;
import com.tec.modules.order.service.mapper.BizOrderMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import com.tec.modules.order.repository.BizOrderRepository;
import com.tec.modules.order.service.BizOrderService;
import com.tec.modules.order.service.dto.BizOrderQueryCriteria;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author tec
* @date 2020-05-27
*/
@Service
//@CacheConfig(cacheNames = "bizOrder")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BizOrderServiceImpl implements BizOrderService {

    private final BizOrderRepository bizOrderRepository;

    private final BizOrderMapper bizOrderMapper;

    public BizOrderServiceImpl(BizOrderRepository bizOrderRepository, BizOrderMapper bizOrderMapper) {
        this.bizOrderRepository = bizOrderRepository;
        this.bizOrderMapper = bizOrderMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(BizOrderQueryCriteria criteria, Pageable pageable){
        Page<BizOrder> page = bizOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(bizOrderMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<BizOrderDto> queryAll(BizOrderQueryCriteria criteria){
        return bizOrderMapper.toDto(bizOrderRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public BizOrderDto findById(Long id) {
        BizOrder bizOrder = bizOrderRepository.findById(id).orElseGet(BizOrder::new);
        ValidationUtil.isNull(bizOrder.getId(),"BizOrder","id",id);
        return this.bizOrderMapper.toDto(bizOrder);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public BizOrderDto create(BizOrder resources) {
        return bizOrderMapper.toDto(bizOrderRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(BizOrder resources) {
        BizOrder bizOrder = bizOrderRepository.findById(resources.getId()).orElseGet(BizOrder::new);
        ValidationUtil.isNull( bizOrder.getId(),"BizOrder","id",resources.getId());
        bizOrder.copy(resources);
        bizOrderRepository.save(bizOrder);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            bizOrderRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BizOrderDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BizOrderDto bizOrder : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单号", bizOrder.getOrderNo());
            map.put("下单时间", bizOrder.getOrderDate());
            map.put("件数", bizOrder.getQuantity());
            map.put("总价", bizOrder.getTotalPrice());
            map.put("邮费", bizOrder.getPostage());
            map.put("支付方", bizOrder.getPostageDirect());
            map.put("是否发货", bizOrder.getSendOff());
            map.put("物流公司", bizOrder.getExpress());
            map.put("物流单号", bizOrder.getExpressNo());
            map.put("备注", bizOrder.getMemo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
